package com.cty.floyd;

import java.util.Arrays;

public class Graph {
    private char[] vertex;
    private int[][] dis;
    private char[][] pre;


    //构造器
    public Graph(char[] vertex,int[][] dis) {
        this.vertex = vertex;
        this.dis = dis;
        this.pre = new char[vertex.length][vertex.length];
        for (int i = 0;i < pre.length;i++) {
            Arrays.fill(pre[i],vertex[i]);
        }
    }

    public void show() {
        for (int i = 0;i < pre.length;i++) {
            System.out.println(Arrays.toString(pre[i]));
        }
        System.out.println("=====前驱表=====");
        for (int i = 0;i < pre.length;i++) {
            System.out.println(Arrays.toString(dis[i]));
        }
    }

    //弗洛伊德算法
    public void floyd() {
        int len;
        for (int k = 0;k < dis.length;k++) {
            for (int i = 0;i < dis.length;i++) {
                for (int j = 0;j < dis.length;j++) {
                    len = dis[i][k] + dis[k][j];
                    if (len < dis[i][j]) {
                        dis[i][j] = len;
                        pre[i][j] = pre[k][j];
                    }
                }
            }
        }
    }
}
